package com.lxb.myforum.controller.admin.sysmgt

import javax.servlet.http.HttpServletRequest

import com.lxb.myforum.base.BaseController
import com.lxb.myforum.domain.ResResult
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.data.domain.PageRequest
import org.springframework.stereotype.Controller
import org.springframework.web.bind.annotation.DeleteMapping
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.PathVariable
import org.springframework.web.bind.annotation.PostMapping
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RequestParam
import org.springframework.web.bind.annotation.ResponseBody

import com.lxb.myforum.domain.PageResponse
import com.lxb.myforum.pojo.Role
import com.lxb.myforum.service.RoleService

/**
 * 角色
 *
 * @author lxb
 */
@Controller("adminRoleCtrl")
@RequestMapping("/admin/roles")
class RoleController @Autowired
constructor(private val roleService: RoleService) : BaseController() {

    @GetMapping(value = ["/", ""])
    fun roles(): String {
        return "/admin/sysmgt/roles/index"
    }

    /**
     *
     * @param page 分页信息
     * @param size 分页信息 page size
     * @return
     */
    @GetMapping(value = ["/all", "/list"])
    @ResponseBody
    fun getRoles(page: Int, size: Int): PageResponse<Role> {
        val rp = PageRequest.of(page - 1, size)
        val roles = roleService.getRoles(rp)
        return PageResponse(0L, "", totalCount = roles.totalElements, rows = roles.content)
    }

    /**
     * 获取该角色相关的权限信息： 包括拥有什么权限、未拥有的权限
     *
     * 如果isOwn = true，那么就是该角色拥有的权限
     *
     * 如果isOwn = false，那么就是该角色未拥有的权限
     * @param roleId 角色id
     */
    @GetMapping(value = ["/{id}/authes"])
    fun getAuthes(req: HttpServletRequest, @PathVariable("id") roleId: Long): String {
        req.setAttribute("ownAuthes", roleService.findOwnAuthorities(roleId))
        req.setAttribute("notOwnAuthes", roleService.findNotOwnAuthorities(roleId))
        return "/admin/sysmgt/roles/roleAuth"
    }

    @DeleteMapping(value = ["/{id}/authes"])
    @ResponseBody
    fun delAuthes(@PathVariable("id") roleId: Long,
                  @RequestParam("authId[]") authIds: Set<Long>): Map<String, Any> {
        val result = roleService.delRoleAuthorities(roleId, authIds)
        if (result["result"] as Int > 0) {
            result["ownAuthes"] = roleService.findOwnAuthorities(roleId)
            result["notOwnAuthes"] = roleService.findNotOwnAuthorities(roleId)
        }
        return result
    }

    @PostMapping(value = ["/{id}/addAuthes"])
    @ResponseBody
    fun addAuthes(@PathVariable("id") roleId: Long,
                  @RequestParam("authId[]") authIds: Set<Long>): Map<String, Any> {
        val result = roleService.addRoleAuthorities(roleId, authIds)
        if (result["result"] as Int > 0) {
            result["ownAuthes"] = roleService.findOwnAuthorities(roleId)
            result["notOwnAuthes"] = roleService.findNotOwnAuthorities(roleId)
        }
        return result
    }

    @DeleteMapping(value = ["/{id}"])
    @ResponseBody
    fun delRole(@PathVariable("id") id: Long): ResResult {
        return roleService.deleteRoleById(id)
    }
}
